#!/bin/bash
###
 # @Author: dvlproad dvlproad@163.com
 # @Date: 2023-02-25 02:04:22
 # @LastEditors: dvlproad dvlproad@163.com
 # @LastEditTime: 2023-02-26 01:59:15
 # @FilePath: /AutoPackage-CommitInfo/bulidScriptCommon/brances_info/brances_info_log/test/tssh_branch_detail_info_result.sh
 # @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
### 
# sh ./tssh_branch_detail_info_result.sh

#var="a/b/c/a/b/多余的"
#var_home=$(echo ${var%/*})  # %/* 表示从右边开始，删除第一个 / 号及右边的字符
#var_path=$(echo ${var##*/}) # */ 表示从左边开始删除最后（最右边）一个 / 号及左边的所有字符
#echo "var_home=${var_home}"
#echo "var_path=${var_path}"
#RESULT_FULL_STRING_SALE_BY_KEY="branch_info_result.Notification.current.full"
#RESULT_FULL_SALE_BY_HOME_KEY=$(echo ${RESULT_FULL_STRING_SALE_BY_KEY%.*})  # %.* 表示从右边开始，删除第一个 . 号及右边的字符
#RESULT_FULL_SALE_BY_PATH_KEY=$(echo ${RESULT_FULL_STRING_SALE_BY_KEY##*.}) # *. 表示从左边开始删除最后（最右边）一个 . 号及左边的所有字符
#echo "RESULT_FULL_SALE_BY_HOME_KEY=${RESULT_FULL_SALE_BY_HOME_KEY}"
#echo "RESULT_FULL_SALE_BY_PATH_KEY=${RESULT_FULL_SALE_BY_PATH_KEY}"
#exit

# 当前【shell脚本】的工作目录
# $PWD代表获取当前路径，当cd后，$PWD也会跟着更新到新的cd路径。这个和在终端操作是一样的道理的
CurrentDIR_Script_Absolute="$( cd "$( dirname "$0" )" && pwd )"
#echo "CurrentDIR_Script_Absolute=${CurrentDIR_Script_Absolute}"
#CommonFun_HomeDir_Absolute3=${CurrentDIR_Script_Absolute}/..
CommonFun_HomeDir_Absolute3=${CurrentDIR_Script_Absolute%/*} # 使用此方法可以避免路径上有..
CommonFun_HomeDir_Absolute2=${CommonFun_HomeDir_Absolute3%/*}
CommonFun_HomeDir_Absolute=${CommonFun_HomeDir_Absolute2%/*}

qscript_path_get_filepath="${CommonFun_HomeDir_Absolute}/qscript_path_get.sh"
qbase_update_json_file_singleString_script_path="$(sh ${qscript_path_get_filepath} qbase update_json_file_singleString)"

commonDateFunctionFilePath="${CommonFun_HomeDir_Absolute}/date/function_date.sh"

Develop_Branchs_FILE_PATH="${CurrentDIR_Script_Absolute}/data/test_data_branch_info.json"
echo "正在引入方法文件(brances_info_log_common.sh)：《source ${CommonFun_HomeDir_Absolute}/brances_info/brances_info_log/brances_info_log_common.sh -commonFunHomeDir \"${CommonFun_HomeDir_Absolute}\" --branch-info-json-file \"${Develop_Branchs_FILE_PATH}\"》"
source ${CommonFun_HomeDir_Absolute}/brances_info/brances_info_log/brances_info_log_common.sh -commonFunHomeDir "${CommonFun_HomeDir_Absolute}" --branch-info-json-file "${Develop_Branchs_FILE_PATH}"

Package_Environment_FILE_PATH="${CurrentDIR_Script_Absolute}/data/test_data_env_info.json"
TEST_DATA_RESULT_FILE_PATH="${CurrentDIR_Script_Absolute}/data/test_data_save_result.json"


buildContainBranchMaps=$(cat ${Develop_Branchs_FILE_PATH} | ${JQ_EXEC} -r '.package_merger_branchs') # -r 去除字符串引号
logBranchIndex=0
iBranchMap=$(echo "${buildContainBranchMaps}" | ${JQ_EXEC} -r ".[$((logBranchIndex))]") # -r 去除字符串引号
branchName=$(echo ${iBranchMap} | ${JQ_EXEC} -r ".name") # -r 去除字符串引号
#echo "----------------------测试的数据buildContainBranchMaps=${buildContainBranchMaps}"


# echo "{}" > ${TEST_DATA_RESULT_FILE_PATH} #清空文件内容,但清空成{}


# shouldTest_core="true"
if [ "${shouldTest_core}" == "true" ]; then
    echo "----------------------------------------------------------------------------0底层方法：getSingleBranchDescription"
    shouldMarkdown='true'  # 暂未支持

    RESULT_ARRAY_SALE_BY_KEY="branch_info_result.Notification.current.branch"
    getSingleBranchDescription -branchMap "${iBranchMap}" --should-markdown "${shouldMarkdown}" -resultSaveToJsonF "${TEST_DATA_RESULT_FILE_PATH}" -resultArrayKey "${RESULT_ARRAY_SALE_BY_KEY}"
    echo "--------------------------------------------0.1分支的描述(含所有概要点)的转义数组字符串Escape_BRANCH_DESCRIPT_STRING_VALUE"
    echo "------------0.1.①${Normal_BRANCH_DESCRIPT_STRING_VALUE}"
    echo "------------0.1.②"
    cat ${TEST_DATA_RESULT_FILE_PATH} | jq ".${RESULT_ARRAY_SALE_BY_KEY}" | jq '.'
    
    echo "{}" > ${TEST_DATA_RESULT_FILE_PATH} #清空文件内容,但清空成{}
    echo "\n\n"
fi



function test_getSingleBranchLog() {
    echo "----------------------------------------------------------------------------1基础层方法:getSingleBranchLog"
    echo "{}" > ${TEST_DATA_RESULT_FILE_PATH} #清空文件内容,但清空成{}

    showBranchLogFlag='true'
    showBranchName='true'
    showBranchTimeLog='all'
    showBranchAtLog='true'
    showBranchTable='false' # 通知也暂时都不显示
    showCategoryName='true' # 通知时候显示
    RESULT_BRANCH_ARRAY_SALE_BY_KEY="branch_info_result.Notification.current.branch"
    getSingleBranchLog -iBranchMap "${iBranchMap}" -showFlag "${showBranchLogFlag}" -showName "${showBranchName}" -showTime "${showBranchTimeLog}" -showAt "${showBranchAtLog}" -commonDateFunctionFilePath "${commonDateFunctionFilePath}" -shouldMD "${shouldMarkdown}" -resultSaveToJsonF "${TEST_DATA_RESULT_FILE_PATH}" -resultArrayKey "${RESULT_BRANCH_ARRAY_SALE_BY_KEY}"
    echo "------------1.1.①第$((logBranchIndex+1))个分支的分支信息如下:\n${Normal_BRANCH_LOG_STRING_VALUE}"
    echo "------------1.1.②"
    echo "{}" > ${TEST_DATA_RESULT_FILE_PATH} #清空文件内容,但清空成{}
    BRANCH_OUTLINES_ELEMENT_LOG_JSON="{\"name\": \"${branchName}\", \"outline\": \"${Normal_BRANCH_LOG_STRING_VALUE}\"}"
    BRANCH_OUTLINES_LOG_JSON="[${BRANCH_OUTLINES_ELEMENT_LOG_JSON}]"
    sh "${CommonFun_HomeDir_Absolute}/update_json_file.sh" -f "${TEST_DATA_RESULT_FILE_PATH}" -k "branch_info_result.Notification.current.branch" -v "${BRANCH_OUTLINES_LOG_JSON}" --skip-value-check "true"
    cat ${TEST_DATA_RESULT_FILE_PATH} | jq ".${RESULT_BRANCH_ARRAY_SALE_BY_KEY}" | jq '.'

    echo "\n\n"
}



        
function test_getCategoryBranchsLog() {
    echo "----------------------------------------------------------------------------3上层方法：按分类获取所有分支信息的整合字符串"
    echo "{}" > ${TEST_DATA_RESULT_FILE_PATH} #清空文件内容,但清空成{}

    showBranchLogFlag='true'
    showBranchName='true'
    showBranchTimeLog='all'
    showBranchAtLog='true'
    showBranchTable='false' # 通知也暂时都不显示
    showCategoryName='true' # 通知时候显示
    shouldMarkdown='true'
    commonDateFunctionFilePath="${CommonFun_HomeDir_Absolute}/date/function_date.sh"
    
    #echo "----------------------测试的数据buildContainBranchMaps=${buildContainBranchMaps}"
    RESULT_BRANCH_ARRAY_SALE_BY_KEY="branch_info_result.Notification.current.branch"
    RESULT_CATEGORY_ARRAY_SALE_BY_KEY="branch_info_result.Notification.current.category"
    RESULT_FULL_STRING_SALE_BY_KEY="branch_info_result.Notification.current.full"           

    echo "----------------------------------------3.2 getAllBranchLogArray_andCategoryThem"
    echo "{}" > ${TEST_DATA_RESULT_FILE_PATH} #清空文件内容,但清空成{}
    getAllBranchLogArray_andCategoryThem -branchMaps "${buildContainBranchMaps}" -showCategoryName "${showCategoryName}" -showFlag "${showBranchLogFlag}" -showName "${showBranchName}" -showTime "${showBranchTimeLog}" -showAt "${showBranchAtLog}" -showTable "${showBranchTable}" -commonDateFunctionFilePath "${commonDateFunctionFilePath}" -shouldMD "${shouldMarkdown}" -resultSaveToJsonF "${TEST_DATA_RESULT_FILE_PATH}" -resultBranchKey "${RESULT_BRANCH_ARRAY_SALE_BY_KEY}" -resultCategoryKey "${RESULT_CATEGORY_ARRAY_SALE_BY_KEY}" -resultFullKey "${RESULT_FULL_STRING_SALE_BY_KEY}"
    echo "------------3.2.②"
    cat ${TEST_DATA_RESULT_FILE_PATH} | jq '.branch_info_result.Notification.current' | jq '.'
    echo "\n\n"


    echo "----------------------------------------3.3 getCategoryBranchsLog"
    TEST_ALL_CATEGORY_BRANCH_STRING=""
    lastLogIndex=0
    #echo "正在执行《getCategoryBranchsLog -categoryJsonF \"${TEST_DATA_RESULT_FILE_PATH}\" -categoryArrayKey \"${RESULT_CATEGORY_ARRAY_SALE_BY_KEY}\" -categoryName 'hotfix' -lastLogIndexInAll "${lastLogIndex}" -showCategoryName \"${showCategoryName}\"》"
    getCategoryBranchsLog -categoryJsonF "${TEST_DATA_RESULT_FILE_PATH}" -categoryArrayKey "${RESULT_CATEGORY_ARRAY_SALE_BY_KEY}" -categoryName 'hotfix' -lastLogIndexInAll "${lastLogIndex}" -showCategoryName "${showCategoryName}"
    #echo "========categoryBranchsLogsResult=${categoryBranchsLogsResult}"
    if [ $? == 0 ] && [ -n "${categoryBranchsLogsResult}" ]; then
        if [ -n "${TEST_ALL_CATEGORY_BRANCH_STRING}" ]; then
            TEST_ALL_CATEGORY_BRANCH_STRING+='\n'
        fi
        TEST_ALL_CATEGORY_BRANCH_STRING+="${categoryBranchsLogsResult}"
    fi
    #echo "------------3.3.①拿一个分类作为所有分类得到的总信息为如下:\n${TEST_ALL_CATEGORY_BRANCH_STRING}"
    echo "------------3.3.②"
    # echo "{}" > ${TEST_DATA_RESULT_FILE_PATH} #清空文件内容,但清空成{}
    RESULT_CATEGORY_ARRAY_SALE_TEST_KEY="branch_info_result.Notification.current.full_test"
    sh ${CommonFun_HomeDir_Absolute}/update_json_file.sh -f "${TEST_DATA_RESULT_FILE_PATH}" -sk "${RESULT_CATEGORY_ARRAY_SALE_TEST_KEY}" -sv "\"${TEST_ALL_CATEGORY_BRANCH_STRING}\"" --skip-value-check "true"
    cat ${TEST_DATA_RESULT_FILE_PATH} | jq ".${RESULT_CATEGORY_ARRAY_SALE_TEST_KEY}" | jq '.'
    echo "\n\n"
    

    echo "----------------------------------------3.4 getAllCategoryBranchLog"
    showCategoryName="true"
    getAllCategoryBranchLog -categoryJsonF "${TEST_DATA_RESULT_FILE_PATH}" -categoryArrayKey "${RESULT_CATEGORY_ARRAY_SALE_BY_KEY}" -showCategoryName "${showCategoryName}" -resultFullKey "${RESULT_FULL_STRING_SALE_BY_KEY}"
    echo "------------3.4.②"
    cat ${TEST_DATA_RESULT_FILE_PATH} | jq ".${RESULT_CATEGORY_ARRAY_SALE_BY_KEY}" | jq '.'


    echo "\n\n"
}


function test_getAllBranchLogString() {
    echo "----------------------------------------------------------------------------4分支完整方法:getAllBranchLogString"
    echo "{}" > ${TEST_DATA_RESULT_FILE_PATH} #清空文件内容,但清空成{}
    branchLogType="Notification"
        
    showBranchLogFlag='true'
    showBranchName='true'
    showBranchTimeLog='all'
    showBranchAtLog='true'
    showBranchTable='false' # 通知也暂时都不显示
    showCategoryName='true' # 通知时候显示
    shouldMarkdown='true'
    commonDateFunctionFilePath="${CommonFun_HomeDir_Absolute}/date/function_date.sh"
    
    #echo "----------------------测试的数据buildContainBranchMaps=${buildContainBranchMaps}"
    RESULT_BRANCH_ARRAY_SALE_BY_KEY="branch_info_result.${branchLogType}.current.branch"
    RESULT_CATEGORY_ARRAY_SALE_BY_KEY="branch_info_result.${branchLogType}.current.category"
    RESULT_FULL_STRING_SALE_BY_KEY="branch_info_result.${branchLogType}.current.full"           

    echo "----------------------------------------4.1 getAllBranchLogString"
    getAllBranchLogString -branchMaps "${buildContainBranchMaps}" -showCategoryName "${showCategoryName}" -showFlag "${showBranchLogFlag}" -showName "${showBranchName}" -showTime "${showBranchTimeLog}" -showAt "${showBranchAtLog}" -showTable "${showBranchTable}" -commonDateFunctionFilePath "${commonDateFunctionFilePath}" -shouldMD "${shouldMarkdown}" -resultSaveToJsonF "${TEST_DATA_RESULT_FILE_PATH}" -resultBranchKey "${RESULT_BRANCH_ARRAY_SALE_BY_KEY}" -resultCategoryKey "${RESULT_CATEGORY_ARRAY_SALE_BY_KEY}" -resultFullKey "${RESULT_FULL_STRING_SALE_BY_KEY}"
    echo "------------4.1.②"
    echo "-------------.branch_info_result.${branchLogType}.current-------------"
    cat ${TEST_DATA_RESULT_FILE_PATH} | jq ".branch_info_result.${branchLogType}.current" | jq '.'
    
    echo "\n"
    echo "----------------------------------------处理后文件最后的完整内容"
    cat ${TEST_DATA_RESULT_FILE_PATH} | jq '.'

    echo "\n\n"
}



function test_getCurrent_getLastOnline() {
    echo "----------------------------------------------------------------------------5顶层方法1:getCurrentBranchLog"
    echo "{}" > ${TEST_DATA_RESULT_FILE_PATH} #清空文件内容,但清空成{}

    REQUEST_FOR_LOG_TYPE_ARRAY=("Notification" "Pgyer")
    sh ${CommonFun_HomeDir_Absolute}/brances_info/brances_info_log/brances_info_log.sh -envInfoF "${Package_Environment_FILE_PATH}" --request-for-log-types "${REQUEST_FOR_LOG_TYPE_ARRAY[*]}" -comScriptHomeDir "${CommonFun_HomeDir_Absolute}" -resultSaveToJsonF "${TEST_DATA_RESULT_FILE_PATH}"
    
    
    branchLogType="Notification" # Notification / Pgyer
    getCurrentBranchLog -devBranchsJsonF "${Develop_Branchs_FILE_PATH}" -branchLogFor "${branchLogType}" -envJsonF "${Package_Environment_FILE_PATH}" -resultSaveToJsonF "${TEST_DATA_RESULT_FILE_PATH}"    
    #echo "------------5.1.①当前打包的所有分支信息如下:\n${currentBranchLogStringResult}"
    echo "------------5.1.②"
    cat ${TEST_DATA_RESULT_FILE_PATH} | jq '.branch_info_result.Notification.current' | jq '.full'
    cat ${TEST_DATA_RESULT_FILE_PATH} | jq '.branch_info_result.Notification.current' | jq '.full_slice'
    echo "\n\n"

    
    echo "----------------------------------------------------------------------------5顶层方法2:getLastOnlineVersionLog"
    History_Versions_FILE_PATH="${CurrentDIR_Script_Absolute}/data/test_data_history_version.json"
    branchLogType="Notification" # Notification / Pgyer
    getLastOnlineVersionLog -historyVersionJsonF "${History_Versions_FILE_PATH}" -branchLogFor ${branchLogType} -envJsonF "${Package_Environment_FILE_PATH}" -resultSaveToJsonF "${TEST_DATA_RESULT_FILE_PATH}"
    #echo "------------5.2.①上次上线打包的所有分支信息如下:\n${lastOnlineVersionLogStringResult}"
    echo "------------5.2.②"
    cat ${TEST_DATA_RESULT_FILE_PATH} | jq '.branch_info_result.Notification.lastOnline' | jq '.full'
    cat ${TEST_DATA_RESULT_FILE_PATH} | jq '.branch_info_result.Notification.lastOnline' | jq '.full_slice'
    
    
    echo "\n\n"
}


function test_initLastAllChangeLog() {
    echo "----------------------------------------------------------------------------6最终方法:initLastAllChangeLog"
    echo "{}" > ${TEST_DATA_RESULT_FILE_PATH} #清空文件内容,但清空成{}
    # 设置执行命令的前提： 记录当前分支的文件路径、 历史分支路径、分支计算结果要保存的路径
    function updateEnvKeyStringValue() {
        EnvKey=$1
        EnvValue=$2
        #echo "正在执行命令(更新路径到JSON文件中)：《sh \"${qbase_update_json_file_singleString_script_path}\" -jsonF \"${Package_Environment_FILE_PATH}\" -k \"${EnvKey}\" -v \"${EnvValue}\"》"
        sh "${qbase_update_json_file_singleString_script_path}" -jsonF "${Package_Environment_FILE_PATH}" -k "${EnvKey}" -v "${EnvValue}"
        if [ $? != 0 ]; then
            exit 1
        fi
    }
    updateEnvKeyStringValue "developBranchsJsonFile" "${Develop_Branchs_FILE_PATH}"
    
    History_Versions_FILE_PATH="${CurrentDIR_Script_Absolute}/data/test_data_history_version.json"
    updateEnvKeyStringValue "historyVersionJsonFile" "${History_Versions_FILE_PATH}"
    
    updateEnvKeyStringValue "branchsResultJsonFile" "${TEST_DATA_RESULT_FILE_PATH}"

    # # 正式执行命令：
    REQUEST_FOR_LOG_TYPE_ARRAY=("Notification" "Pgyer")
    sh ${CommonFun_HomeDir_Absolute}/brances_info/brances_info_log/brances_info_log.sh -envInfoF "${Package_Environment_FILE_PATH}" --request-for-log-types "${REQUEST_FOR_LOG_TYPE_ARRAY[*]}" -comScriptHomeDir "${CommonFun_HomeDir_Absolute}" -resultSaveToJsonF "${TEST_DATA_RESULT_FILE_PATH}"
    if [ $? != 0 ]; then
        exit 1
    fi

    #echo "------------6.1.①当前打包的所有分支信息如下:\n${FullChangeLogResult}"
    echo "------------6.1.②查看存入值，直接通过 cat 和 jquery"
    REQUEST_FOR_LOG_TYPE_COUNT=${#REQUEST_FOR_LOG_TYPE_ARRAY[*]}
    for((i=0;i<${REQUEST_FOR_LOG_TYPE_COUNT};i++));
    do
        REQUEST_FOR_LOG_TYPE_STRING=${REQUEST_FOR_LOG_TYPE_ARRAY[i]}
        echo "----------------------------.branch_info_result.${REQUEST_FOR_LOG_TYPE_STRING}----------------------------"
        cat ${TEST_DATA_RESULT_FILE_PATH} | jq ".branch_info_result.${REQUEST_FOR_LOG_TYPE_STRING}" | jq '.'
        echo "----------------------------.branch_info_result.${REQUEST_FOR_LOG_TYPE_STRING}.all.slice----------------------------"
        cat ${TEST_DATA_RESULT_FILE_PATH} | jq ".branch_info_result.${REQUEST_FOR_LOG_TYPE_STRING}.all.slice" | jq '.'
    done
    
    # echo "\n"
    # echo "----------------------------------------处理后文件最后的完整内容"
    # cat ${TEST_DATA_RESULT_FILE_PATH} | jq '.'
    
    echo "\n\n"
}


# test_getSingleBranchLog
# test_getCategoryBranchsLog
# test_getAllBranchLogString
# test_getCurrent_getLastOnline
test_initLastAllChangeLog
